import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_application_1/pages/data/home_page_data.dart';
import 'package:flutter_application_1/pages/home/home_vm.dart';
import 'package:flutter_application_1/route/routes.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';
import 'package:provider/provider.dart';
import '../web_view_page.dart';
import '../../route/route_utils.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  // 6.新加代码
  HomeViewModel viewModel = HomeViewModel();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    viewModel.getBanner();
  }
  // 5.删除之前代码

  @override
  Widget build(BuildContext context) {
    // 7.return  ChangeNotifierProvider 泛型为 6定义HomeViewModel
    return ChangeNotifierProvider<HomeViewModel>(
      create: (context) => viewModel,
      child: Scaffold(
        body: SafeArea(
            child: SingleChildScrollView(
          child: Column(
            children: [_banner(), _listdata()],
          ),
        )),
      ),
    );
  }

  Widget _listdata() {
    print('list打印');
    return ListView.builder(
      physics: NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      itemCount: 100,
      itemBuilder: (BuildContext context, int index) {
        return _litviewitem();
      },
    );
  }

  Widget _banner() {
    //7.组件刷新
    return Consumer<HomeViewModel>(builder: (context, vm, child) {
      print('banner打印');
      return Container(
        height: 150,
        child: Swiper(
          itemCount: 3,
          indicatorLayout: PageIndicatorLayout.COLOR, // 指示器
          autoplay: true,
          pagination: const SwiperPagination(),
          control: const SwiperControl(),
          itemBuilder: (context, index) {
            return Container(
                width: double.infinity,
                height: 150,
                child: Container(
                  width: double.infinity,
                  height: 150,
                  // title:vm.bannerlist[index].title,
                  color: Colors.red,
                ));
          },
        ),
      );
    });
  }

  // 抽离item组件
  Widget _litviewitem() {
    return GestureDetector(
      onTap: () => {
        // Navigator.pushNamed(context, RoutePath.WebViewPage)
        // RouteUtils.pushForNamed(
        //     context, WebViewPage(title: 'WebView') as String)
        RouteUtils.pushForNamed(context, RoutePath.WebViewPage,
            arguments: {'name': '使用路由set传值'})
        // Navigator.push(
        //     context,
        //     MaterialPageRoute(
        //         builder: (context) => WebViewPage(
        //               title: '标sss题',
        //             )))
      },
      child: Container(
        margin: EdgeInsets.only(top: 5, bottom: 5, left: 10, right: 10),
        padding: EdgeInsets.only(top: 5, bottom: 5, left: 10, right: 10),
        decoration: BoxDecoration(
            border: Border.all(color: Colors.grey, width: 0.5),
            borderRadius: BorderRadius.all(Radius.circular(6))),
        child: const Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ClipRRect(
                  child: Image(
                    width: 30,
                    height: 30,
                    fit: BoxFit.cover,
                    image: NetworkImage(
                        'https://img1.baidu.com/it/u=2219384865,204572674&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1067'),
                  ),
                ),

                SizedBox(width: 10), //边距
                Text(
                  '作者',
                  style: TextStyle(
                    color: Colors.black,
                  ),
                ),
                Expanded(child: SizedBox()),
                Text(
                  '2024-08-01',
                  style: TextStyle(
                    color: Colors.black,
                  ),
                ),
                Padding(padding: const EdgeInsets.only(left: 10)), //边距
                Text(
                  '置顶',
                  style: TextStyle(
                      color: Colors.blue, fontWeight: FontWeight.bold),
                ),
              ],
            ),
            Text(
                '标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题标题'),
            Row(
              children: [
                Text(
                  '分类',
                  style: TextStyle(color: Colors.green, fontSize: 12),
                ),
                Expanded(child: SizedBox()),
                // Image.asset(
                //   'assets/images/icon1.jpg',
                //   width: 30,
                //   height: 30,
                // ),
                Image(
                    width: 30,
                    height: 30,
                    image: NetworkImage(
                        'https://img1.baidu.com/it/u=2219384865,204572674&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1067')),
              ],
            )
          ],
        ),
      ),
    );
  }
}
